/*******************************************************************************
 *
 *    Copyright (c) Baina Info Tech Co. Ltd
 *
 *    Free Music Downloder
 *    
 *    URLCheckUtil
 *    TODO File description or class description.
 *
 *    @author: zuochen
 *    @since:  2011-9-13
 *    @version: 1.0
 *
 ******************************************************************************/

package com.sdk.url.util;

import java.net.MalformedURLException;
import java.net.URL;

import com.sdk.util.Log;

/**
 * URLCheckUtil of Free Music Downloder.
 * 
 * @author zuochen
 * 
 */

public class URLCheckUtil {
	/**
	 * Type the music form Android system surppoted.
	 */
	private static final String[] MUSIC_FORM = { ".mp3", ".ogg", ".wmv" };
	private static final String TAG = URLCheckUtil.class.getSimpleName();

	/**
	 * Check whether the given url is legal.
	 * 
	 * @param url
	 *            the geiven url.
	 * @return whether the url is legal.
	 */
	public static boolean isURLLegal(String url) {
		boolean isLegal = false;
		if (url == null) {
			return isLegal;
		}
		try {
			@SuppressWarnings("unused")
			final URL checkUrl = new URL(url);
			isLegal = true;
		} catch (final MalformedURLException e) {
			Log.d(TAG, " [isURLLegal] the input url isn't legal");
			isLegal = false;
		}
		return isLegal;
	}

	/**
	 * Check whether should to download the given url.
	 * 
	 * @param url
	 *            the given url
	 * @return true should download.
	 */
	public static boolean isShouldDownloadURL(String url) {
		boolean isShouldDownload = false;
		if (null == url) {
			return isShouldDownload;
		}
		isShouldDownload = isMusicURl(url) ? true : false;
		return isShouldDownload;
	}

	/**
	 * Check whether the url is a music resource link. We just check the last
	 * the substring of url, if the substring contain the music form we typed,
	 * see {@link #MUSIC_FORM}, we affirm the url is a music resouce.
	 * @param url
	 *            the given url
	 * @return true, if the url is a music resouce link.
	 */

	public static boolean isMusicURl(String url) {
		for (int i = 0; i < MUSIC_FORM.length; i++) {
			if (url.endsWith(MUSIC_FORM[i])) {
				return true;
			}
		}
		return false;
	}
}
